#include <Reg51.h>
#include "delay.h"
#include "LCD1602.h"
#include <string.h>
#include <stdio.h>

unsigned char IntegerToStr(int dat, unsigned char *str);

void main()
{
	//long dat = -12345678; //unsigned long 最大4294967295 signed int -2147483648-+2147483648
	int dat = -1234;
	unsigned char str[6]; //"4294967295\0"
	
	float num;
	float num2;
	unsigned char lcdbuf[10];
	int temp;
	
	LCDInit();
	DelayXms(10);
	IntegerToStr(dat, str);
	LCDShowStr(0, 0, str);
	
	num = 1.252;
	num2 = 12.23445666;
	temp = num * 1000;
	LCDSetPosition(0,1);
	LCDWriteData(temp / 1000 + '0');
	LCDWriteData('.');
	LCDWriteData(temp / 100 % 10 + '0');
	LCDWriteData(temp / 10 % 10 + '0');
	LCDWriteData(temp % 10 + '0');
	
	sprintf(lcdbuf, "%0.7f",num2);
	LCDShowStr(5, 1, lcdbuf);
	while(1);
}

unsigned char IntegerToStr(int dat, unsigned char *str)
{
	unsigned char i;//数据的索引
	unsigned char len;//数据长度的返回
	unsigned char buf[6];//存放字符串
	if (dat < 0)
	{
		dat = -dat;//如果是负数，取绝对值
		*str++ = '-';
		len++;
	}
	do
	{
		buf[i++] = dat % 10 + '0';//dat % 10 + 0x30
		dat /= 10;
	}while(dat > 0);
	len += i;
	while(i-- > 0)
	{
		*str++ = buf[i];
	}
	*str = '\0';
	return len;
}